<?php

namespace App\Http\Controllers\Admin\Cashier;

use App\Models\Cashier\Member;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Cashier\Sale;
use App\Models\Cashier\Store;

class SaleController extends Controller
{
    public function __construct()
    {
        view()->share([
            '_cashier' => 'active open',
            '_sale' => 'active',
            'stores' => Store::orderBy('id', 'desc')->get(),
        ]);
    }

    /***
     * 加载销售统计页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        return view('admin.cashier.sale.index');
    }


    public function store_sale()
    {
        return view('admin.cashier.sale.store_sale');
    }


    public function month_sale()
    {
        return view('admin.cashier.sale.month_sale');
    }


    /***
     * 会员购买记录
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function member_sale(Request $request)
    {
        $where = function ($query) use ($request) {
            //按会员名称
            if ($request->has('name') and $request->name != '') {
                $member = Member::where('name', $request->name)->first();
                if ($member) {
                    $query->where('member_id', $member->id);
                }
            }

            //按门店
            if ($request->has('store_id') and $request->store_id != '-1') {
                $query->where('store_id', $request->store_id);
            }
        };
        //查出所有会员消费记录
        $member_records = Sale::with('sale_products.product', 'member', 'store')
            ->where($where)->orderBy('pay_time', 'desc')
            ->paginate(env('pageSize'));
//        return $member_records['pay_type'];
        return view('admin.cashier.sale.member_sale', compact('member_records'));
    }


}
